<?php
/*
 * Copyright notice
 * 
 * (c) 2009 MH-Dev.de, Martin Hesse <info@mh-dev.de>
 * 
 * This script is part of the MH-Dev.-Board project. The MH-Dev.-Board 
 * is free software; you can redistribute it and/or modify it under the 
 * terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 3 of the License, 
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty 
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU 
 * General Public License along with this program; if not, 
 * see http://www.gnu.org/licenses/.
 */


/**
 * Backend UserController
 *
 * @author	Martin Hesse <info@mh-dev.de>
 * @package	MH-Dev.-Board
 */
class Backend_UserController extends Zend_Controller_Action
{
	private $registry; // registry
	private $auth; // user-values
	private $settings; // board-settings
	private $baseUrl; // helper
	
	/*
	 * Init class vars and stuff
	 */
	public function init()
	{		
		// registry
		$this->registry = Zend_Registry::getInstance();
		
		// settings
		$this->settings	= $this->registry->get('settings');
		
		// helper
		$baseUrl		= new MHDEV_View_Helper_BaseUrl();
		$this->baseUrl	= $baseUrl->BaseUrl();
		
		// auth
		$auth = Zend_Auth::getInstance();
		$this->view->auth = $auth->hasIdentity();
    	
		if($auth->hasIdentity())
		{
			$this->view->auth_role 			= $auth->getIdentity()->role;
			$this->view->username 			= $auth->getIdentity()->username;
			$this->view->auth_uid			= $auth->getIdentity()->uid;
			$this->view->unread_messages	= $auth->getIdentity()->count_unread_messages;
		}
		else
		{
			$this->view->username 	= $this->registry->get('lang')->_('guest');
		}

		$this->auth = $auth;
    	
		// lang
		$this->view->translate = $this->registry->get('lang');
		
		// include js
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/jquery-1.3.2.min.js');
		$this->view->headScript()->appendFile($this->baseUrl . '/static/js/backend.js');
	}
	
	
	
	/*
	 * List user
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function indexAction()
	{
		// vars
		$count			= 10; 
		$page			= intval($this->getRequest()->getParam('page', 0));
		
		// db
		$user			= new User();
		
		// query
		$getUser		= $user->fetchAll(null, 'create_date DESC', $count);
		
		// paginator
		$paginator	= Zend_Paginator::factory($getUser->toArray());
        $paginator->setItemCountPerPage($count);
        $paginator->setCurrentPageNumber($page);
        
		// view
		$this->view->count		= $getUser->count();
		$this->view->results	= $paginator;
	}
	
	
	
	/*
	 * Edit a user
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function editAction()
	{
		// param
		$user_uid	= $this->_request->getParam('uid', 0);

		// form
		$form 		= new UserForm();
		
		// db
		$user		= new User();
		$groups		= new Groups();
		$layouts	= new Layouts();
		
		// query
		$query			= $user->find($user_uid);
		$queryGroups	= $groups->fetchAll();
		$getLayouts		= $layouts->fetchAll();
		
		if($query->count() > 0)
		{
			$res		= $query->toArray();
			$formData 	= $this->_request->getPost();
				
			if($this->_request->isPost() && $form->isValid($formData))
			{
				if($formData['is_deleted'] == 1 && $user_uid != 1)
				{
					$user->delete('uid=' . $user_uid);
					$this->_redirect('/backend/user.html');
				}
				
				$passwd = $form->getValue('password');
				
				$form->removeElement('password');
				$form->removeElement('is_deleted');
				$form->removeElement('submit');

				if(strlen($passwd) > 0)
				{
					$user->update(array('password' => md5($passwd)), 'uid=' . $user_uid);
				}
				
				$user->update($form->getValues(), 'uid=' . $user_uid);
				
				$this->_redirect('/backend/user-' . $user_uid . '.html');
			}
			else
			{
				// fill select-fields
				$selGroup = $form->getElement('group_uid');
				
				if($queryGroups->count() > 0)
				{
					foreach($queryGroups->toArray() AS $result)
					{
						$selGroup->addMultiOption($result['uid'], $result['title']);
					}
				}
				
				// fill layout-field
				$getLayout	= $form->getElement('layout');
				
				foreach($getLayouts->toArray() AS $layout)
				{
					$getLayout->addMultiOption($layout['key'], $layout['title']);
				}
			
				$form->populate($res[0]);
			}
		}
		else
		{
			$this->_redirect('/backend/user.html');
		}

		// view
		$this->view->form = $form;
	}
	
	
	
	/*
	 * handle ajax request for backend navigation
	 * 
	 * @param null
	 * 
	 * @return void
	 */
	public function requestAction()
	{
		// disable layout
		Zend_Layout::getMvcInstance()->disableLayout();
		
		// post
		$postData	= $this->_request->getPost();
		
		// db
		$user		= new User();
		
		// helper
		$view		= new Zend_View();
		
		if($this->_request->isPost())
		{
			foreach($postData AS $postVar => $value)
			{
				if($postVar != 'uid')
				{
					if(is_int($value))
					{
						$clean_value	= int_val($value);
					}
					elseif(is_string($value))
					{
						$clean_value	= $view->escape($value);
					}
					$user->update(array($postVar => $clean_value), 'uid=' . $postData['uid']);
				}
			}
		}
	}
}
?>